package sz.szsmk.citizencard.util;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.util.ArrayList;

import sz.szsmk.citizencard.R;

/**
 * Copyright (c)2021/6/25
 * author: hxh
 * created on: 2021/6/25 10:31
 * description: 权限弹框提示类
 */
public class PermissionUtil {

    private static View view;
    private static Dialog dialog;
    private static View agree;


    public static void checkPermission(Activity context, String permission,String text){
        Dialog dialog = showDialog(context,text);
        agree.setOnClickListener(v -> {
            if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                dialog.dismiss();
                ArrayList<String> list = new ArrayList<>();
                list.add(permission);
                ActivityCompat.requestPermissions(context, list.toArray(new String[0]), 1);

            }if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                dialog.dismiss();
                ArrayList<String> list = new ArrayList<>();
                list.add(permission);
                ActivityCompat.requestPermissions(context, list.toArray(new String[0]), 1);

            }if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                dialog.dismiss();
                ArrayList<String> list = new ArrayList<>();
                list.add(permission);
                ActivityCompat.requestPermissions(context, list.toArray(new String[0]), 1);
            }if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                dialog.dismiss();
                ArrayList<String> list = new ArrayList<>();
                list.add(permission);
                ActivityCompat.requestPermissions(context, list.toArray(new String[0]), 1);
            }
        });


    }


    @SuppressLint("InflateParams")
    public static Dialog showDialog(Activity context, String text){
        dialog = new Dialog(context, R.style.Translucent_NoTitle);
        view = LayoutInflater.from(context).inflate(R.layout.permissiom_dialog, null);
        TextView title=view.findViewById(R.id.fuwuyingsi);
        ImageView close = view.findViewById(R.id.policy_refuse);
        agree = view.findViewById(R.id.policy_agree);
        TextView textView = view.findViewById(R.id.policy_text);
        title.setText("权限申请");
        textView.setText(text+"");

        close.setOnClickListener(v -> {
            dialog.dismiss();
        });
        agree.setOnClickListener(v -> {
            dialog.dismiss();
        });
        dialog.setCancelable(false);
        dialog.setContentView(view);
        dialog.show();

        return dialog;
    }

}
